package com.wms.controller;

import cn.hutool.core.bean.BeanUtil;
import com.wms.dto.WarehouseAreaDto;
import com.wms.dto.WarehouseAreaIdAndNameDto;
import com.wms.dto.WarehouseAreaPageDto;
import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.pojo.WarehouseArea;
import com.wms.service.WarehouseAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/warehousearea")
public class WarehouseAreaController {
    @Autowired
    private WarehouseAreaService warehouseAreaService;

    @GetMapping
    public Result queryPage(WarehouseAreaPageDto dto){
        PageResult pageResult = warehouseAreaService.queryPage(dto);
        return Result.success(pageResult);
    }

    //新增
    @PostMapping
    public Result saveWarehouseArea(@RequestBody WarehouseAreaDto dto){
        warehouseAreaService.saveWarehouseArea(dto);
        return Result.success();
    }

    //回显
    @GetMapping("/{id}")
    public Result getWarehouseAreaById(@PathVariable Integer id){
        WarehouseArea warehouseArea = warehouseAreaService.getById(id);
        WarehouseAreaDto dto = BeanUtil.toBean(warehouseArea, WarehouseAreaDto.class);
        return Result.success(dto);
    }

    //修改
    @PutMapping
    public Result updateWarehouseAreaById(@RequestBody WarehouseAreaDto dto){
        warehouseAreaService.updateWarehouseAreaById(dto);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public Result deleteWarehouseAreaById(@PathVariable Integer id){
        warehouseAreaService.removeById(id);
        return Result.success();
    }

    //查询所有库区
    @GetMapping("/all")
    public Result queryAll(){
        List<WarehouseArea> warehouseAreas = warehouseAreaService.list();
        List<WarehouseAreaIdAndNameDto> list = new ArrayList<>();
        for (WarehouseArea area : warehouseAreas) {
            WarehouseAreaIdAndNameDto warehouseAreaIdAndNameDto = BeanUtil.toBean(area, WarehouseAreaIdAndNameDto.class);
            list.add(warehouseAreaIdAndNameDto);
        }
        return Result.success(list);
    }
}
